home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / prog / cfuncs.zip / CPYFIL.C < prev    next >
Text File  |  1991-08-01  |  1KB  |  79 lines

  1. #include <stdlib.h>
  2. #include <io.h>
  3. #include <fcntl.h>
  4. #include <sys\stat.h>
  5. #include <alloc.h>
  6. #include <string.h>
  7.  
  8. int cpyfil(char *n1, char *n2)
  9.  
  10. {
  11.  
  12. #if defined(__COMPACT__) || defined(__LARGE__) || defined(__HUGE__)
  13.     unsigned long free_mem;
  14. #else
  15.     unsigned free_mem;
  16. #endif
  17.   unsigned block_size;
  18.   long file_size;
  19.   char *buf;
  20.   int f1, f2;
  21.  
  22.   char mes[50];
  23.  
  24.  
  25.     if ( (f1 = open(n1, O_RDONLY | O_BINARY )) == -1)
  26.     {
  27.     strcpy(mes, n1);
  28.     message( strcat( mes, " NOT FOUND"), 2);
  29.     return(1);
  30.     }
  31.  
  32.     if ( (f2 = open(n2, O_CREAT | O_WRONLY | O_TRUNC | O_BINARY, S_IWRITE )) == -1)
  33.     {
  34.     strcpy(mes, n2);
  35.     message( strcat( mes, " COULD NOT OPEN"), 2);
  36.     return(2);
  37.     }
  38.  
  39.   free_mem = coreleft();
  40.  
  41.   if((file_size = filelength(f1))==-1)
  42.     return(3);
  43.  
  44.   block_size = min(0xFFF0, min(free_mem, file_size));
  45.   if((buf = malloc(block_size))==NULL)
  46.     return(4);
  47.  
  48.   while (file_size > 0)
  49.    {
  50.      block_size = min(0xFFF0, min(free_mem, file_size));
  51.  
  52.      if(read(f1, buf, block_size) == -1)
  53.         return(5);
  54.      if(write(f2, buf, block_size) == -1)
  55.         return(6);
  56.  
  57.      file_size -= block_size;
  58.     }
  59.  
  60.   free(buf);
  61.   close(f1);
  62.   close(f2);
  63.  
  64.   return(0);
  65.  
  66. }
  67. /*
  68. #include <time.h>
  69. main()
  70. {
  71.     clock_t start, end;
  72.  
  73.     start = clock();
  74.     cpyfil("\\tc\\tc.exe", "tc.exe");
  75.     end = clock();
  76.  
  77.     printf("time was %f\n", (end-start) / CLK_TCK);
  78.  
  79. } */